package com.example.springboot.biz;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
public class MailServiceImpl{

    // 邮件发送器,  spring-boot-starter-mail 创建的bean
    @Autowired
    private JavaMailSender mailSender;

    // 自定义的发送者地址
    @Value("${spring.mail.fromMail.addr}")
    private String from;

    /**
     * 发送邮件
     * @param to        接收者
     * @param subject   标题
     * @param content   内容
     */
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }
}

